﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Practices.Prism.ViewModel;
using System.ComponentModel.Composition;
using Microsoft.Practices.Prism.Events;
using System.Collections.Generic;
using System.ServiceModel.DomainServices.Client;
using Microsoft.Practices.Prism.Commands;
using SailHero.Soft.FrameWork.Models;
using UI.Web.SL.Demo.RIAServices.Web;

namespace UI.Web.SL.Demo.Modules.Log
{
    [Export(typeof(LogListViewModel))]
    public class LogListViewModel : NotificationObject
    {
        private IEnumerable<LOG> _logList;
        private LogDContext _logDContext = new LogDContext();
        private LoadOperation<LOG> _loadLogListOperation;

        [ImportingConstructor]
        public LogListViewModel(IEventAggregator eventAggregator)
        {
            eventAggregator.GetEvent<UI.Web.SL.Demo.Infrastructure.Events.LoggingCompletedEvent>().Subscribe(LoadLog, ThreadOption.UIThread);
            LoadLog(null);
        }

        public void LoadLog(object o)
        {
            _loadLogListOperation = _logDContext.Load(_logDContext.GetLogsQuery());
            _loadLogListOperation.Completed += new EventHandler(_loadLogListOperation_Completed);
        }

        void _loadLogListOperation_Completed(object sender, EventArgs e)
        {
            this.LogList = _loadLogListOperation.Entities;
        }

        public IEnumerable<LOG> LogList
        {
            get { return _logList; }
            set
            {
                _logList = value;
                base.RaisePropertyChanged("LogList");
            }
        }
    }
}
